package com.vilen.cms.service;

import com.vilen.basic.model.Pager;
import com.vilen.cms.dao.IChannelDao;
import com.vilen.cms.dao.IGroupDao;
import com.vilen.cms.dao.IUserDao;
import com.vilen.cms.model.*;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by vilen on 2017/2/2.
 */
@Service("groupService")
public class GroupService implements IGroupService {

    public IGroupDao groupDao;
    public IUserDao userDao;
    public IChannelDao channelDao;

    public IGroupDao getGroupDao() {
        return groupDao;
    }

    @Inject
    public void setGroupDao(IGroupDao groupDao) {
        this.groupDao = groupDao;
    }

    public IUserDao getUserDao() {
        return userDao;
    }

    @Inject
    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    public IChannelDao getChannelDao() {
        return channelDao;
    }

    @Inject
    public void setChannelDao(IChannelDao channelDao) {
        this.channelDao = channelDao;
    }

    @Override
    public void add(Group group) {
        groupDao.add(group);
    }

    @Override
    public void delete(int id) {
        List<User> users = userDao.listGroupUsers(id);
        if (users != null && users.size() > 0) {
            throw new CmsException("删除的组中还有用户,不能删除");
        }
        groupDao.delete(id);
    }

    @Override
    public Group load(int id) {
        return groupDao.load(id);
    }

    @Override
    public void update(Group group) {
        groupDao.update(group);
    }

    @Override
    public List<Group> listGroup() {
        return groupDao.listGroup();
    }

    @Override
    public Pager<Group> findGroup() {
        return groupDao.findGroup();
    }

    @Override
    public void deleteGroupUsers(int gid) {
        groupDao.deleteGroupUsers(gid);
    }

    @Override
    public void addGroupChannel(int gid, int cid) {
        Group g = groupDao.load(gid);
        Channel c = channelDao.load(cid);
        if (c == null || g == null) {
            throw new CmsException("要添加的组频道关联对象不存在!");
        }
        groupDao.addGroupChannel(g,c);
    }

    @Override
    public GroupChannel loadGroupChannel(int gid, int cid) {
        return groupDao.loadGroupChannel(gid, cid);
    }

    @Override
    public void clearGroupChannel(int gid) {
        groupDao.clearGroupChannel(gid);
    }

    @Override
    public void deleteGroupChannel(int gid, int cid) {
        groupDao.deleteGroupChannel(gid,cid);
    }

    @Override
    public List<Integer> listGroupChannelIds(int gid) {
        return groupDao.listGroupChannelIds(gid);
    }

    @Override
    public List<ChannelTree> generateGroupChannelTree(int gid) {
        return groupDao.generateGroupChannelTree(gid);
    }

    @Override
    public List<ChannelTree> generateUserChannelTree(int uid) {
        return groupDao.generateUserChannelTree(uid);
    }
}
